<?php

namespace app\demo\controller;

use yunj\Controller;
use app\demo\enum\State;
use app\demo\service\Example as Service;
use app\demo\validate\example\Edit as EditValidate;

class Example extends Controller {

    public function lists() {
        $builder1 = YT('general_example')
            ->state(State::tabs())
            ->filter(function ($state) {
                return Service::instance()->filterByListGeneralExample($state);
            })
            ->toolbar(function ($state) {
                return Service::instance()->toolbarByListGeneralExample($state);
            })
            ->defaultToolbar(function ($state) {
                return Service::instance()->defaultToolbarByListGeneralExample($state);
            })
            ->cols(function ($state) {
                return Service::instance()->colsByListGeneralExample($state);
            })
            ->count(function ($filter) {
                return Service::instance()->countByListGeneralExample($filter);
            })
            ->items(function ($limit_start, $limit_length, $filter, $sort) {
                return Service::instance()->itemsByListGeneralExample($limit_start, $limit_length, $filter, $sort);
            })
            ->event(function ($event, $ids) {
                return Service::instance()->eventByListGeneralExample($event, $ids);
            })
            ->import(function ($state) {
                return Service::instance()->importByListGeneralExample($state);
            });
        $builder1->assign($this);

        $args2 = [
            "state" => ['all' => '全部'],
            "page" => false,
            "defaultToolbar" => function ($state) {
                return Service::instance()->defaultToolbarByListSortExample($state);
            },
            "cols" => function ($state) {
                return Service::instance()->colsByListSortExample($state);
            },
            "count" => function ($filter) {
                return Service::instance()->countByListSortExample($filter);
            },
            "items" => function ($limit_start, $limit_length, $filter, $sort) {
                return Service::instance()->itemsByListSortExample($limit_start, $limit_length, $filter, $sort);
            },
            "event" => function ($event, $ids) {
                return Service::instance()->eventByListSortExample($event, $ids);
            }
        ];
        $builder2 = YT('sort_example', $args2);
        $builder2->assign($this);

        return $this->fetch();
    }

    public function add() {
        $builder = YF('general_example')
            ->tab(['base' => '演示字段', 'other' => '其他字段'])
            ->field(function ($tab) {
                return Service::instance()->fieldByAddGeneralExample($tab);
            })
            ->button(["back", 'reload', 'clear', 'reset', 'submit'])
            ->submit(function ($data, $tab_data) {
                return Service::instance()->submitByAddGeneralExample($data, $tab_data);
            });
        $builder->assign($this);
        return $this->fetch();
    }

    public function edit() {
        $args = [
            "tab" => ['base' => '基础', 'other' => '其他'],
            "field" => function ($tab) {
                return Service::instance()->fieldByEditGeneralExample($tab);
            },
            "fieldValidate" => function () {
                return new EditValidate();
            },
            "button" => ["back", 'reload', 'clear', 'reset', 'submit'],
            "load" => function () {
                return Service::instance()->loadByEditGeneralExample();
            },
            "submit" => function ($data, $tab_data) {
                return Service::instance()->submitByEditGeneralExample($data, $tab_data);
            },
        ];
        $builder = YF('general_example', $args);
        $builder->assign($this);
        return $this->fetch();
    }

    public function friendOptions() {
        return Service::instance()->friendOptions();
    }

    public function tips() {
        return redirect_tips();
    }

}